<?php

namespace app\api\controller\kuyou\applet;

use app\api\model\kuyou\applet\Acceptance as Production;
use think\Db;

/**
 * 验收单接口
 */
class Acceptances extends Common
{
    protected $noNeedRight = 'getDetail';

    /**
     * 获取验收单
     * @ApiMethod (POST)
     * @param int $page 页码
     * @param int $limit 每页条数
     * @param string $type_name 分类名称
     */
    public function index()
    {
        if ($this->request->isPost()) {
            $page = $this->request->post('page');
            $page = empty($page)?1:$page;
            $limit = $this->request->post('limit');
            $limit = empty($limit)?10:$limit;

            $purchase_code = $this->request->post('purchase_code');
            $where = [];
            if(!empty($purchase_code)){
                $where['purchase_code'] = ['like','%'.$purchase_code.'%'];
            }
            $status = $this->request->post('status');
            if($status!=null && $status!==''){
                $where['status'] = $status;
            }
            $examine_status = $this->request->post('examine_status');
            if($examine_status!=null && $examine_status!==''){
                $where['examine_status'] = $examine_status;
            }

            $list = Db::name('kuyou_acceptance')->where($where)
                ->page($page,$limit)
                ->field('id,purchase_code,purchase_time,finish_number,number,all_money,code_type,chase_code')->select();
            $count = Db::name('kuyou_acceptance')->where($where)->count();
            foreach ($list as $list_k=>$list_v){
                if ($list_v['code_type']==1){
                    //加工单
                    $production_id = Db::name('kuyou_production')->where(['purchase_code'=>$list_v['chase_code']])->value('id');
                    $goods_list = Db::name('kuyou_production_info')->where(['production_id'=>$production_id,'type'=>2])->field('goods_name,number')->select();
                    $list[$list_k]['outsourcing_free'] = 0;
                    $list[$list_k]['hour_free'] = 0;
                    $list[$list_k]['wages_free'] = 0;
                    $production = Db::name('kuyou_production')->where(['purchase_code'=>$list_v['chase_code']])->find();
                    $production_info = Db::name('kuyou_production_info')->where(['production_id'=>$production['id'],'type'=>'1'])->select();

                    $hour_free = 0;$wages_free = 0;
                    if($production){
                        $production_id = Db::name('kuyou_plan')->where(['purchase_code'=>$production['plan_code']])->value('id');
                        $goods_list = Db::name('kuyou_plan_info')->where(['plan_id'=>$production_id])->field('goods_name,number,goods_code')->select();
                        foreach ($goods_list as $goods_list_k=>$goods_list_v){
                            $bom_id = Db::name('kuyou_bom')->where(['goods_code'=>$goods_list_v['goods_code']])->value('id');
                            $procedure = Db::name('kuyou_bom_procedure')->where(['bom_id'=>$bom_id])->select();
                            foreach ($procedure as $procedure_k=>$procedure_v){
                                $hour_free += ($procedure_v['wages']*$procedure_v['hours']);
                            }
                            $this_wages = Db::name('kuyou_bom')->where(['goods_code'=>$goods_list_v['goods_code']])->value('wages');
                            $wages_free += $this_wages;
                        }
                    }else{
                        $goods_list = [];
                    }
                    $list[$list_k]['hour_free'] = $hour_free;
                    $list[$list_k]['wages_free'] = $wages_free;

                    $material = Db::name('kuyou_material')->where(['production_code'=>$list_v['chase_code']])->value('id');
                    $material_info = Db::name('kuyou_material_info')->where(['production_id'=>$material])->select();
                }else{
                    //委外加工单
                    $plan = Db::name('kuyou_outsourcing')->where(['purchase_code'=>$list_v['chase_code']])->find();
                    $production_info = Db::name('kuyou_outsourcing_info')->where(['production_id'=>$plan['id']])->select();
                    $list[$list_k]['outsourcing_free'] = $plan['free_money'];
                    $hour_free = 0;$wages_free = 0;
                    if($plan){
                        $production_id = Db::name('kuyou_plan')->where(['purchase_code'=>$plan['plan_code']])->value('id');
                        $goods_list = Db::name('kuyou_plan_info')->where(['plan_id'=>$production_id])->field('goods_name,number,goods_code')->select();
                        foreach ($goods_list as $goods_list_k=>$goods_list_v){
                            $bom_id = Db::name('kuyou_bom')->where(['goods_code'=>$goods_list_v['goods_code']])->value('id');
                            $procedure = Db::name('kuyou_bom_procedure')->where(['bom_id'=>$bom_id])->select();
                            foreach ($procedure as $procedure_k=>$procedure_v){
                                $hour_free += ($procedure_v['wages']*$procedure_v['hours']);
                            }
                            $this_wages = Db::name('kuyou_bom')->where(['goods_code'=>$goods_list_v['goods_code']])->value('wages');
                            $wages_free += $this_wages;
                        }
                    }else{
                        $goods_list = [];
                    }
                    $list[$list_k]['hour_free'] = $hour_free;
                    $list[$list_k]['wages_free'] = $wages_free;

                    $material = Db::name('kuyou_material')->where(['plan_code'=>$list_v['chase_code']])->value('id');
                    $material_info = Db::name('kuyou_material_info')->where(['production_id'=>$material])->select();
                }
                $str = '';
                foreach ($goods_list as $goods_list_k=>$goods_list_v){
                    $str .= $goods_list_v['goods_name'].'*'.$goods_list_v['number'].' ';
                }
                $list[$list_k]['goods_info'] = $str;

                $production_free = 0;
                foreach ($production_info as $production_info_k=>$production_info_v){
                    $production_free += $production_info_v['money'];
                }
                $list[$list_k]['production_free'] = $production_free;

                $material_free = 0;
                foreach ($material_info as $material_info_k=>$material_info_v){
                    $material_free += $material_info_v['money'];
                }
                $list[$list_k]['material_free'] = $material_free;

                $cost = $list[$list_k]['outsourcing_free']+$list[$list_k]['hour_free']+$list[$list_k]['wages_free']+$list[$list_k]['production_free']-$material_free;
                $list[$list_k]['cost_free'] = $cost;
            }

            $result['list'] = $list;
            $result['count'] = $count;

            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 获取验收单详情
     * @ApiMethod (POST)
     * @param int $id
     */
    public function getdetail()
    {
        if ($this->request->isPost()) {
            $id = $this->request->post('id');
            $result = Production::getDetail($id);
            if($result['status']==200){
                $this->success('查询成功',$result['msg'],200);
            }else{
                $this->error($result['msg'],'',$result['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 获取生产原料
     * @ApiMethod (POST)
     * @param int $id
     */
    public function pickinfo()
    {
        if ($this->request->isPost()) {
            $code_type = $this->request->post('code_type');
            $purchase_code = $this->request->post('purchase_code');
            $result = Production::pickinfo($code_type,$purchase_code);
            if($result['status']==200){
                $this->success('查询成功',$result['msg'],200);
            }else{
                $this->error($result['msg'],'',$result['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 添加同步商品
     * @ApiMethod (POST)
     * @param array $detail
     */
    public function acceptance()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            foreach ($data as $k=>$v){
                $data[$k] = is_array($v)?$v:html_entity_decode($v);
            }
            $ret = Production::acceptance($data);
            if($ret['status']==200){
                $this->success('操作成功',$ret['msg'],200);
            }else{
                $this->error($ret['msg'],'',$ret['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 添加验收单
     * @ApiMethod (POST)
     * @param array $detail
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            foreach ($data as $k=>$v){
                $data[$k] = is_array($v)?$v:html_entity_decode($v);
            }
            $ret = Production::addDetail($data);
            if($ret['status']==200){
                $this->success('添加成功',$ret['msg'],200);
            }else{
                $this->error($ret['msg'],'',$ret['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 更新验收单
     * @ApiMethod (POST)
     * @param array $detail
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            foreach ($data as $k=>$v){
                $data[$k] = is_array($v)?$v:html_entity_decode($v);
            }
            $ret = Production::editDetail($data);
            if($ret['status']==200){
                $this->success('更新成功','',200);
            }else{
                $this->error($ret['msg'],'',$ret['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 删除
     * @ApiMethod (POST)
     * @param int $id
     */
    public function del()
    {
        if ($this->request->isPost()) {
            $id = $this->request->post('id');
            $ret = Production::delDetail($id);
            if($ret['status']==200){
                $this->success('删除成功',$ret['msg'],200);
            }else{
                $this->error($ret['msg'],'',$ret['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 审核
     * @ApiMethod (POST)
     * @param int $id
     */
    public function examine()
    {
        if ($this->request->isPost()) {
            $ids = $this->request->post('id');
            $type = $this->request->post('type');
            if($type=='examine'){
                $ret = Production::examine($ids);
                $msg = '审核成功';
            }else{
                $ret = Production::examinef($ids);
                $msg = '反审核成功';
            }
            if($ret['status']==1){
                $this->success($msg,$ret['msg'],200);
            }else{
                $this->error($ret['msg'],'',$ret['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

}
